package com.hangzhou.xyj.ui.fragment;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.ui.activity.LoginActivity;
import com.hangzhou.xyj.ui.adapter.HomeCommunityVPAdapter;
import com.hangzhou.xyj.ui.dialog.TipsDialog;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;
import com.hangzhou.xyj.ui.widget.PagerSlidingTabStrip;

/**
 * 社群
 *
 * @author neo.duan
 */
public class CommunityFragment extends BaseFragment implements OnClickListener {
    private PagerSlidingTabStrip mViewPagerTab;
    private ViewPager mViewPager;
    private HomeCommunityVPAdapter mAdapter;
    private int mLastItem;//上次选中的Item
    private OnItemChangeListener mListener;

    public CommunityFragment() {
    }

    public void setText(String text) {
        initDatas();
    }

    @Override
    public void initTop() {
        enableTop(false);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.fragment_community);
    }

    @Override
    public void initViews() {
        mViewPagerTab = findView(R.id.community_tabs);
        mViewPager = findView(R.id.vp_main_community);

        mViewPager.setAdapter(mAdapter = new HomeCommunityVPAdapter(getFragmentManager()));
        //向ViewPager绑定PagerSlidingTabStrip
        mViewPagerTab.setViewPager(mViewPager);
        mViewPager.setCurrentItem(0);
        mLastItem = 0;
    }

    @Override
    public void initDatas() {

    }

    public void setOnItemChangeListener(OnItemChangeListener listener) {
        mListener = listener;
    }

    /**
     * 重载下拉刷新方法，使用自己的加载方式
     */
    @Override
    public void doRefresh() {
        int currentItem = mViewPager.getCurrentItem();
        BaseFragment fragment = (BaseFragment) mAdapter.getItem(currentItem);
        fragment.initDatas();
    }

    @Override
    public void initListeners() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                //选中了“关注动态” 且 未登录
                if (i == 1 && XYJApplication.getUserInfo() == null) {
                    TipsDialog dialog = new TipsDialog(mViewPager.getContext(), "提示", "需要登录才能操作，是否登录？", "确定", "取消") {
                        @Override
                        protected void OnClickNegativeButton() {
                            this.dismiss();
                            mViewPager.setCurrentItem(mLastItem);
                        }

                        @Override
                        protected boolean OnClickPositiveButton() {
                            IntentManager.getInstance().goActivity(mContext, new Intent(mContext, LoginActivity.class));
                            return true;
                        }
                    };
                    dialog.show();
                    return;
                } else if (i == 1) {
                    //刷新数据
                    mAdapter.mFollowFragment.initDatas();
                }
                mLastItem = i;
                mListener.itemChange(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    public interface OnItemChangeListener {
        void itemChange(int position);
    }

}
